<template>
  <div>
    <div style="margin-bottom: 30px">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>新闻后台管理</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div style="margin: 10px 0">
      <el-input
          style="width: 200px; margin-right: 15px"
          placeholder="请输入用户名"
          suffix-icon="el-icon-search"
          v-model="username"
      ></el-input>
      <el-input
          style="width: 200px; margin-right: 15px"
          placeholder="请输入邮箱"
          suffix-icon="el-icon-message"
          v-model="email"
          class="ml-5"
      ></el-input>
      <el-input
          style="width: 200px; margin-right: 15px"
          placeholder="请输入地址"
          suffix-icon="el-icon-position"
          v-model="address"
          class="ml-5"
      ></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>


    <el-table
        :data="tableData"
        border
        stripe
        @selection-change="handleSelectionChange"
    >
      <el-table-column prop="id" label="ID" width="80"></el-table-column>
      <el-table-column
          align="center"
          prop="avatarUrl"
          width="140"
          label="用户头像">
        <template v-slot="scope">
          <el-image
              style="width: 60px; height: 60px;border-radius: 10px"
              :src="scope.row.avatarUrl"
              :preview-src-list="[scope.row.avatarUrl]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column
          prop="username"
          label="用户名"
          width="140"
      ></el-table-column>

      <el-table-column
          prop="nickname"
          label="昵称"
          width="120"
      ></el-table-column>
      <el-table-column prop="email" label="邮箱" width="140"></el-table-column>
      <el-table-column prop="phone" label="电话" width="140"></el-table-column>
      <el-table-column
          prop="address"
          label="地址"
          width="140"
      ></el-table-column>
      <el-table-column
          prop="role"
          label="是否管理员"
          width="140"
      >
        <template v-slot="scope">
          <el-switch v-if="scope.row.id==124" v-model="scope.row.role" disabled active-color="#13ce66"
                     inactive-color="#ccc"
                     @change="changeEnable(scope.row)"></el-switch>
          <el-switch v-else v-model="scope.row.role" active-color="#13ce66" inactive-color="#ccc"
                     @change="changeEnable(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="500" align="center">
        <template v-slot="scope">
          <div v-if="scope.row.id==124"></div>
          <div v-else>
            <el-button type="success" size="small" @click="handleEdit(scope.row)"
            >编辑 <i class="el-icon-edit"></i
            ></el-button>
            <el-button
                type="warning"
                size="small"
                @click="resetPassword(scope.row.id)"
            >重置密码 <i class="el-icon-s-tools"></i
            ></el-button>

            <el-popconfirm
                class="ml-5"
                confirm-button-text="确定"
                cancel-button-text="我再想想"
                icon="el-icon-info"
                icon-color="red"
                title="您确定删除吗？"
                @confirm="del(scope.row.id)"
            >
              <el-button
                  type="danger"
                  size="small"
                  style="margin-left: 15px"
                  slot="reference"
              >删除 <i class="el-icon-remove-outline"></i
              ></el-button>
            </el-popconfirm>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[3, 5, 7, 9]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog title="用户信息" :visible.sync="dialogFormVisible" width="30%">
      <el-form label-width="80px" size="small">
        <el-form-item label="用户名">
          <el-input :disabled="true" v-model.trim="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="form.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {deleteUser, addOrUpdateUser, searchUser,resetPassword} from '@/api/User'

export default {
  name: "Home",
  data() {
    return {
      dialogFormVisible: false,
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 9,
      username: "",
      email: "",
      address: "",
      form: {},
      multipleSelection: [],
    };
  },
  created() {
    // 请求分页查询数据
    this.load();
  },
  methods: {
    resetPassword(id) {
      //确定重置？
      this.$confirm('确定重置密码为123456吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //重置密码
        resetPassword(id).then((res) => {
          if (res==true) {
            this.$message.success("重置成功");
            this.load();
          } else {
            this.$message.error("重置失败");
          }
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消重置'
        });
      });
    },
    del(id) {
      deleteUser(id).then((res) => {
        if (res) {
          this.$message.success("删除成功");
          this.load();
        } else {
          this.$message.error("删除失败");
        }
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleEdit(row) {
      // this.form = row;
  this.form=Object.assign({},row);
      this.dialogFormVisible = true;
    },
    handleAdd() {
      this.dialogFormVisible = true;
      this.form = {};
    },
    save() {
      addOrUpdateUser(this.form).then((res) => {
        if (res.code == 200) {
          this.$message.success("保存成功");
          this.dialogFormVisible = false;
          this.load();
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    reset() {
      this.username = "";
      this.email = "";
      this.address = "";
      this.load();
      this.$message.success("重置成功");
    },
    changeEnable(row) {
      addOrUpdateUser(row).then((res) => {
        if (res) {
          this.$message.success("操作成功");
        } else {
          this.$message.error("操作失败");
        }
      });
    },
    load() {
      searchUser(this.pageNum, this.pageSize, this.username, this.email, this.address).then((res) => {
        this.tableData = res.records;
        this.total = res.total;
      });
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum;
      this.load();
    },
  },
};
</script>

<style>
</style>
